
// 接口地址：https://www.apifox.cn/apidoc/shared-36f0cbc0-9064-44bc-850a-805e263bf494/api-25827426



const dom = (ele) => document.querySelector(ele)
axios.defaults.baseURL = 'http://124.223.14.236:3001/api'

async function fn() {
    //省
    const { data: res } = await axios.get('/city/province')
    const ProStr = res.data.map(item => `<option value="${item.province}">${item.name}</option>`).join('')
    dom('#province').innerHTML = '<option value="">请选择省份</option>' + ProStr
    dom('#province').value = '34'

    //市
    const { data: cityData } = await axios.get('/city/city', { params: { provinceCode: '34' } })
    const cityStr = cityData.data.map(item => `<option value="${item.city}">${item.name}</option>`).join('')
    dom('#city').innerHTML = '<option value="">请选择城市</option>' + cityStr
    dom('#city').value = '01'

    //区
    const { data: areaData } = await axios.get('/city/area', { params: { provinceCode: '34', cityCode: '01' } })
    const areaStr = areaData.data.map(item => `<option value="${item.area}">${item.name}</option>`).join('')
    dom('#county').innerHTML = '<option value="">请选择区县</option>' + areaStr
    dom('#county').value = '02'
}
fn()

dom('#province').addEventListener('change', async function () {
    //市
    const { data: cityData } = await axios.get('/city/city', { params: { provinceCode: this.value } })
    const cityStr = cityData.data.map(item => `<option value="${item.city}">${item.name}</option>`).join('')
    dom('#city').innerHTML = '<option value="">请选择城市</option>' + cityStr
    dom('#county').innerHTML = '<option value="" >请选择区县</option>'
})

dom('#city').addEventListener('change', async function () {
    //区
    const { data: areaData } = await axios.get('/city/area', { params: { provinceCode: dom('#province').value, cityCode: this.value } })
    const areaStr = areaData.data.map(item => `<option value="${item.area}">${item.name}</option>`).join('')
    dom('#county').innerHTML = '<option value="">请选择区县</option>' + areaStr
})